<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
	exit ('Access Denied');
}

/**
 * 每个 Logic 对应一个业务逻辑处理单元
 * 每个 Logic 必须继承自 BaseLogic
 * 每个 Logic 对应一个或多个 View/DAO
 */
class ArticleLogic extends TinBaseLogic {

	private $artDao;
	private $artCatDao;

	// 每个 Logic 需要有一个构造函数
	public function ArticleLogic() {
		parent::__construct();	
		$this->logger->className = "ArticleLogic";
		$this->artDao = Tin::getDAO("AssignTable","ARTICLE");
		$this->artCatDao = Tin::getDAO("AssignTable","ARTICLE_CAT");
	}
	
	public function __destruct(){
		$this->artDao = null;
	}
	
	public function getCatAll($cols, $cond, $order=null){
		return $this->artCatDao->selectAll($cols, $cond, $order);
	}
	public function getCatItem($cols, $cond){
		return $this->artCatDao->selectRowByCond($cols, $cond);
	}
	/** 添加分类记录 */
	public function addCatItem($data){
		$map = Tin :: createDataMap();
		$map->addEntry('NAME', $data['name'], DB::VARCHAR);
		$map->addEntry('PARENT_ID', $data['parent_id'], DB::INT);
		if($data['order_num'])$map->addEntry('SORT_NUM', $data['order_num'], DB::INT);
		if($data['state']) $map->addEntry('STATE', $data['state'], DB::INT);
		try{
			$res = $this->artCatDao->insert($map, false, true);
			return $res;
		}catch(Exception $e){
            $this->logger->error("addCatItem:".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 更新指定分类记录 */
	public function updateCatItem($data, $cond){
		$map = Tin :: createDataMap();
		if($data['name']) $map->addEntry('NAME', $data['name'], DB::VARCHAR);
		if($data['parent_id']) $map->addEntry('PARENT_ID', $data['parent_id'], DB::INT);
		if($data['order_num']) $map->addEntry('SORT_NUM', $data['order_num'], DB::INT);
		if($data['state']) $map->addEntry('STATE', $data['state'], DB::INT);
		if($data['arrparentid']) $map->addEntry('arrparentid', $data['arrparentid'], DB::VARCHAR);
		if($data['arrchildid']) $map->addEntry('arrchildid', $data['arrchildid'], DB::VARCHAR);
		if($data['child'])  $map->addEntry('child', $data['child'], DB::VARCHAR);
		try{
			$res = $this->artCatDao->updateByCond($map, $cond, false);
			return $res;
		}catch(Exception $e){
            $this->logger->error("updateCatItem".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 删除指定分类记录 */
	public function delCatItem($cond, $params = false){
		return $this->artCatDao->deleteByCond($cond, $params);
	}

	/** 根据指定条件获取分页列表数据 */
	public function getArtList($cols, $cond, $order_by  = null, $page = null, $size = null){
        $res = array();
		$res["data"] = $this->artDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
		if ($page){
			$res["count"] = $this->artDao->getCountByCond($cond);
		}
		return $res;
	}
	public function getArtItem($cols, $cond){
		return $this->artDao->selectRowByCond($cols, $cond);
	}
	/** 添加文章 */
	public function addArtItem($data){
		$map = Tin :: createDataMap();
		$map->addEntry('TITLE', $data['title'], DB::VARCHAR);
		$map->addEntry('CATID', $data['catid'], DB::INT);
                $map->addEntry('USER_ID', $data['user_id'], DB::INT);
		$map->addEntry('CONTENT', $data['content'], DB::TEXT);
		if($data['thumb']) $map->addEntry('THUMB', $data['thumb'], DB::VARCHAR);
		if($data['order_num']) $map->addEntry('ORDER_NUM', $data['order_num'], DB::INT);
		if($data['state']) $map->addEntry('STATE', $data['state'], DB::INT);
		$map->addEntry('CREATE_TIME', 'now()', DB::DATETIME);
		try{
			$res = $this->artDao->insert($map, false, true);
			return $res;
		}catch(Exception $e){
                        $this->logger->error("addItem:".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 更新指定菜单记录 */
	public function updateArtItem($data, $cond){
		$map = Tin :: createDataMap();
		if($data['title']) $map->addEntry('TITLE', $data['title'], DB::VARCHAR);
		if($data['catid']) $map->addEntry('CATID', $data['catid'], DB::INT);
		if($data['content']) $map->addEntry('CONTENT', $data['content'], DB::TEXT);
		if($data['thumb']) $map->addEntry('THUMB', $data['thumb'], DB::VARCHAR);
		if($data['order_num']) $map->addEntry('ORDER_NUM', $data['order_num'], DB::INT);
		if($data['state']) $map->addEntry('STATE', $data['state'], DB::INT);
		$map->addEntry('CREATE_TIME', 'now()', DB::DATETIME);
		try{
			$res = $this->artDao->updateByCond($map, $cond, false);
			return $res;
		}catch(Exception $e){
            $this->logger->error("updateFunsItem".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 删除指定菜单记录 */
	public function delArtItem($cond, $params = false){
		return $this->artDao->deleteByCond($cond, $params);
	}
        
        public function getArtList2($cols, $cond, $order_by  = null, $page = null, $size = null){
                $res = array();
		$res["data"] = $this->artDao->selectByCond($cols, $cond . " " . $order_by, $page, $size);
	        $res["count"] = $this->artDao->getCountByCond($cond);
		return $res;
	}
}//End Class
?>
